package cn.xxy.converter;

import cn.xxy.TypeConverter;

import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @author xxy
 * @date 2024/11/20
 */
public class LocalDateTimeConverter implements TypeConverter {


    @Override
    public boolean supports(Class<?> fieldType) {
        return fieldType.equals(LocalDateTime.class);
    }

    @Override
    public Object apply(Class<?> fieldType, Object value) {
        if(value instanceof LocalDateTime) {
            return value;
        }
        else if(value instanceof LocalDate) {
            return ((LocalDate)value).atStartOfDay();
        }
        else if(value instanceof LocalTime) {
            return ((LocalTime)value).atDate(LocalDate.now());
        }
        else if(value instanceof Date) {
            return ((Date)value).toLocalDate().atStartOfDay();
        }
        else if(value instanceof Timestamp) {
            return ((Timestamp)value).toLocalDateTime();
        }
        else if(value instanceof Time) {
            return ((Time)value).toLocalTime().atDate(LocalDate.now());
        }
        return null;
//        return switch (value) {
//            case LocalDateTime localDateTime -> localDateTime;
//            case LocalDate localDate -> localDate.atStartOfDay();
//            case LocalTime localTime -> localTime.atDate(LocalDate.now());
//            case Date sqlDate -> sqlDate.toLocalDate().atStartOfDay();
//            case Timestamp timestamp -> timestamp.toLocalDateTime();
//            case Time time -> time.toLocalTime().atDate(LocalDate.now());
//            default -> null;
//        };
    }
}
